﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace BridgeShips
{
    public class ChooseShipType : MonoBehaviour
    {
        private Toggle[] m_BridgeShip_Toggles;
        private Transform m_bgContent;
        private string url = "";
        private ReadXML m_readXML;
        private bool[] m_Values;
        private CanvasGroup canvasGroup;
        
        // Use this for initialization
        void Start()
        {
            url = Application.dataPath + "/Resources/Configs/ShipTypes.txt";
            m_bgContent = transform.Find("bg");
            canvasGroup = transform.GetComponent<CanvasGroup>();
            m_BridgeShip_Toggles = new Toggle[16];
            #region 初始化Toggle
            for (int i = 0; i < m_BridgeShip_Toggles.Length; i++)
            {
                m_BridgeShip_Toggles[i] = m_bgContent.GetChild(i).Find("Toggle1").GetComponent<Toggle>();
                m_BridgeShip_Toggles[i].isOn = false;
                m_bgContent.GetChild(i).Find("Toggle2").GetComponent<Toggle>().isOn = false;
                //SetShipsType(i + 1, true);
            }
          

            m_BridgeShip_Toggles[0].onValueChanged.AddListener(OnToggle0Click);
            m_BridgeShip_Toggles[1].onValueChanged.AddListener(OnToggle1Click);
            m_BridgeShip_Toggles[2].onValueChanged.AddListener(OnToggle2Click);
            m_BridgeShip_Toggles[3].onValueChanged.AddListener(OnToggle3Click);
            m_BridgeShip_Toggles[4].onValueChanged.AddListener(OnToggle4Click);
            m_BridgeShip_Toggles[5].onValueChanged.AddListener(OnToggle5Click);
            m_BridgeShip_Toggles[6].onValueChanged.AddListener(OnToggle6Click);
            m_BridgeShip_Toggles[7].onValueChanged.AddListener(OnToggle7Click);
            m_BridgeShip_Toggles[8].onValueChanged.AddListener(OnToggle8Click);
            m_BridgeShip_Toggles[9].onValueChanged.AddListener(OnToggle9Click);
            m_BridgeShip_Toggles[10].onValueChanged.AddListener(OnToggle10Click);
            m_BridgeShip_Toggles[11].onValueChanged.AddListener(OnToggle11Click);
            m_BridgeShip_Toggles[12].onValueChanged.AddListener(OnToggle12Click);
            m_BridgeShip_Toggles[13].onValueChanged.AddListener(OnToggle13Click);
            m_BridgeShip_Toggles[14].onValueChanged.AddListener(OnToggle14Click);
            m_BridgeShip_Toggles[15].onValueChanged.AddListener(OnToggle15Click);

            #endregion
            m_readXML = new ReadXML();
            m_Values = new bool[16];
            if (m_readXML.CheckExistFiles(url))
            {
                for (int i = 1; i <= 16; i++)
                {
                    var value = m_readXML.XmlReadShipTypes(i, url);
                    m_BridgeShip_Toggles[i - 1].isOn = bool.Parse(value);
                    SetShipsType(i, bool.Parse(value));
                    m_Values[i - 1] = bool.Parse(value);

                }
            }
            else
            {
                m_readXML.XmlCreateShipTypes(url);
                for (int i = 0; i < m_Values.Length; i++)
                {
                    m_Values[i] = true;
                }
            }
           // canvasGroup.alpha = 0;
           // transform.GetComponent<RectTransform>().localScale = Vector3.zero;
        }
        private void Update()
        {
            if (ShipsDictionary.Instance.shipDic.Count<=0)
            {
                //if (Input.GetKeyDown(KeyCode.P))
                //{
                //    if (canvasGroup.alpha == 0)
                //    {
                //        canvasGroup.alpha = 1;
                //        transform.GetComponent<RectTransform>().localScale = Vector3.one;
                //    }
                //    else
                //    {
                //        canvasGroup.alpha = 0;
                //        transform.GetComponent<RectTransform>().localScale = Vector3.zero;
                //    }
                //}
            }
           
        }
        #region 添加的监听事件
        private void OnToggle0Click(bool isOn) {
            SetShipsType(1, isOn);
        }
        private void OnToggle1Click(bool isOn)
        {
            SetShipsType(2, isOn);
        }
        private void OnToggle2Click(bool isOn)
        {
            SetShipsType(3, isOn);
        }
        private void OnToggle3Click(bool isOn)
        {
            SetShipsType(4, isOn);
        }
        private void OnToggle4Click(bool isOn)
        {
            SetShipsType(5, isOn);
        }
        private void OnToggle5Click(bool isOn)
        {
            SetShipsType(6, isOn);
        }
        private void OnToggle6Click(bool isOn)
        {
            SetShipsType(7, isOn);
        }
        private void OnToggle7Click(bool isOn)
        {
            SetShipsType(8, isOn);
        }
        private void OnToggle8Click(bool isOn)
        {
            SetShipsType(9, isOn);
        }
        private void OnToggle9Click(bool isOn)
        {
            SetShipsType(10, isOn);
        }
        private void OnToggle10Click(bool isOn)
        {
            SetShipsType(11, isOn);
        }
        private void OnToggle11Click(bool isOn)
        {
            SetShipsType(12, isOn);
        }
        private void OnToggle12Click(bool isOn)
        {
            SetShipsType(13, isOn);
        }
        private void OnToggle13Click(bool isOn)
        {
            SetShipsType(14, isOn);
        }
        private void OnToggle14Click(bool isOn)
        {
            SetShipsType(15, isOn);
        }
        private void OnToggle15Click(bool isOn)
        {
            SetShipsType(16, isOn);
        }

        #endregion
        private void SetShipsType(int id,bool isOn) {
          
            ShipTypes shipTypes = ShipTypes.StraddleType;

            if (isOn)
            {
                shipTypes = ShipTypes.StraddleType;
            }
            else
            {
                shipTypes = ShipTypes.LineType;
            }
            //Debug.LogError(id + "=====" + shipTypes);
            ShipsDictionary.Instance.UpdateShipPrefabricateType((uint)id, shipTypes);

            if (m_Values[id-1] !=isOn)
            {
                m_readXML.XmlUpdateShipTypes(url, id, isOn.ToString());
            }
        }
    }
}

